package com.tuanzi.loan.web.bean.family.largeamount;

import com.tuanzi.loan.business.entity.family.LoanLargeNewDelivery;
import com.tuanzi.loan.business.entity.family.LoanWholeSaleComment;
import com.tuanzi.loan.business.service.family.LargeNewDeliveryService;
import com.tuanzi.loan.business.service.family.LoanLargeAmountPlusRecordService;
import com.tuanzi.loan.business.service.family.LoanWholeSaleCommentService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.CommRegionNewInfo;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: liangZongXi
 * Create time: 15:15 2019/7/9
 * Description:
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LargeNewDeliveryInfoBean extends JSFInfoBean<LoanLargeNewDelivery> {
    private static final long serialVersionUID = 2691426337046220620L;

    private List<LoanWholeSaleComment> loanWholeSaleCommentList;
    private List<CommRegionNewInfo> commRegionNewInfoList;
    private List<String> selectCity;

    @Autowired
    private LoanWholeSaleCommentService loanWholeSaleCommentService;
    @Autowired
    private LoanLargeAmountPlusRecordService loanLargeAmountPlusRecordService;
    @Autowired
    private LargeNewDeliveryService largeNewDeliveryService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void init() {
        String idStr = getParamFromRequest("id");
        long id = NumberUtils.toLong(idStr);
        if(0 != id){ //编辑
            LoanLargeNewDelivery one = largeNewDeliveryService.findOne(id);
            if(null == one){
                forceRedirect("/views/family/WholeSaleManage/largeNewDeliveryList.xhtml");
                return;
            }

            this.$ = one;
            initSelectCity($.getCityStr());

        }else{ //新增
            changeStatusToAdd();
            $ = new LoanLargeNewDelivery();
        }

        initBaseInfo();
    }

    private void initSelectCity(String cityStr){
        if(StringUtils.isNotBlank(cityStr)){
            this.selectCity = Arrays.stream(cityStr.split(",")).collect(Collectors.toList());
        }
    }

    private void initBaseInfo(){
        if(null == loanWholeSaleCommentList){
            loanWholeSaleCommentList = loanLargeAmountPlusRecordService.getWaiTuiProduct();
        }
        if(null == commRegionNewInfoList){
            commRegionNewInfoList = loanWholeSaleCommentService.findAllRegionInfo();
        }
    }

    public String onReturn(){
        return redirect("/views/family/WholeSaleManage/largeNewDeliveryList.xhtml");
    }

    public void save(){
        if(selectCity == null || selectCity.size() == 0){
            addErrorMessage("请选择地区");
            return;
        }
        if(selectCity.contains("默认") && selectCity.size() > 1){
            addErrorMessage("默认选择只能单独使用");
            return;
        }

        String cityStr = selectCity.stream().collect(Collectors.joining(","));
        this.$.setCityStr(cityStr);

        this.$ = largeNewDeliveryService.saveOrUpdate($);
        stringRedisTemplate.delete(LoanRedisKey.LOAN_LARGE_OUTER_NEW_DELIVERY_LIST);
        changeStatusToView();
        addSuccessfullyMessage();
    }

}
